//
//  CustomeNavigationBar.m
//  Neka
//
//  Created by yu on 2018/4/16.
//  Copyright © 2018年 ma c. All rights reserved.
//

#import "CustomeNavigationBar.h"
#define KIsiPhoneX ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1125, 2436), [[UIScreen mainScreen] currentMode].size) : NO)
@implementation CustomeNavigationBar
{
    CGFloat _subviewsY;
}

+ (CustomeNavigationBar *)customeNavigationBar {
    CustomeNavigationBar *bar = [[CustomeNavigationBar alloc] initWithFrame:CGRectMake(0, 0, K_WIDTH, 64)];
    return bar;
    
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"nav_bg"]];
        if (KIsiPhoneX) {
            self.frame = CGRectMake(0, 0, K_WIDTH, 88);
            _subviewsY = 40;
        }else {
            _subviewsY = 20;
        }
        [self addSubview:self.titleLabel];
        [self addSubview:self.backBtn];
        [self addSubview:self.closeBtn];
        [self addSubview:self.shareBtn];
    }
    return self;
}

#pragma mark -- 设置Title
- (void)showTitle:(NSString *)title {
    if (title.length > 0) {
        _titleLabel.text = title;
    }
}


- (void)showBackBtn:(BOOL)showBack closeBtn:(BOOL)showClose shareBtn:(BOOL)showShare {
    
    self.backBtn.hidden = !showBack;
    self.closeBtn.hidden = !showClose;
    self.shareBtn.hidden = !showShare;
}

#pragma mark -- 子视图
- (UILabel *)titleLabel {
    if (_titleLabel == nil) {
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(K_WIDTH / 2 - 80, _subviewsY, 160, 44)];
        _titleLabel.textColor = [UIColor whiteColor];
        _titleLabel.font = [UIFont systemFontOfSize:18];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _titleLabel;
}


- (UIButton *)backBtn {
    if (_backBtn == nil) {
        _backBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, _subviewsY, 64, 44)];
        UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(0, 7, 30, 30)];
        img.image = [UIImage imageNamed:@"fanh"];
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(25, 0, 44, 44)];
        label.text = @"返回";
        label.font = [UIFont systemFontOfSize:16];
        label.textColor = [UIColor whiteColor];
        [_backBtn addSubview:img];
        [_backBtn addSubview:label];
        
        [self.backBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _backBtn;
}

- (UIButton *)closeBtn {
    if (_closeBtn == nil) {
        _closeBtn = [[UIButton alloc] initWithFrame:CGRectMake(64, _subviewsY, 40, 44)];
        _closeBtn.titleLabel.font = [UIFont systemFontOfSize:16];
        [_closeBtn setTitle:@"关闭" forState:UIControlStateNormal];
        [self.closeBtn addTarget:self action:@selector(closeAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _closeBtn;
}

- (UIButton *)shareBtn {
    if (_shareBtn == nil) {
        _shareBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.frame.size.width - 54, _subviewsY, 44, 44)];
        [_shareBtn setTitle:@"分享" forState:UIControlStateNormal];
        [self.shareBtn addTarget:self action:@selector(shareAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _shareBtn;
}
#pragma mark -- 按钮点击事件
- (void)backAction {
    if ([self.delegate respondsToSelector:@selector(customeNavigationBar:clickBackBtn:)]) {
        [self.delegate customeNavigationBar:self clickBackBtn:self.backBtn];
    };
}

- (void)closeAction {
    if ([self.delegate respondsToSelector:@selector(customeNavigationBar:clickCloseBtn:)]) {
        [self.delegate customeNavigationBar:self clickCloseBtn:self.closeBtn];
        
    };
}

- (void)shareAction {
    if ([self.delegate respondsToSelector:@selector(customeNavigationBar:clickShareBtn:)]) {
        [self.delegate customeNavigationBar:self clickShareBtn:self.shareBtn];
    };
}


@end
